//index.js
//获取应用实例
var api = require('../../api.js');
var app = getApp();
var share_count = 0;

Page({
  data: {
    x: wx.getSystemInfoSync().windowWidth,
    y: wx.getSystemInfoSync().windowHeight
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    app.pageOnLoad(this);
    this.loadData(options);
    var page = this;
    var scene = decodeURIComponent(options.scene);
  },
  /**
     * 加载页面数据
     */
  loadData: function (options) {
    var page = this;
    var pages_index_index = wx.getStorageSync('pages_index_index');
    if (pages_index_index) {
      page.setData(pages_index_index);
    }

    page.setData({ image_root: api.image_root})

    app.request({
      url: api.default.home,
      success: function (res) {
        console.log(res)
        if (res != null) {
          // var index_data = {}
          // index_data.data = res[0]
          // console.log(index_data)
          page.setData(res[0]);
          wx.setStorageSync('pages_index_index', res[0]);
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    app.pageOnShow(this);
    // share_count = 0;
    // var store = wx.getStorageSync("store");
    // if (store && store.name) {
    //   wx.setNavigationBarTitle({
    //     title: store.name,
    //   });
    // }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (options) {
    // var page = this;
    // return {
    //   path: "/pages/index/index",
    //   success: function (e) {

    //   }
    // };
  },

  navigatorClick: function (e) {
    var page = this;
    var open_type = e.currentTarget.dataset.open_type;
    var url = e.currentTarget.dataset.url;
    if (!isNaN(url)) {
      wx.makePhoneCall({
        phoneNumber: url, //仅为示例，并非真实的电话号码
      });
      return false;
    }
    if (open_type != 'wxapp')
      return true;
    //console.log(url);
    url = parseQueryString(url);
    url.path = url.path ? decodeURIComponent(url.path) : "";
    console.log("Open New App");
    wx.navigateToMiniProgram({
      appId: url.appId,
      path: url.path,
      complete: function (e) {
        console.log(e);
      }
    });
    return false;

    function parseQueryString(url) {
      var reg_url = /^[^\?]+\?([\w\W]+)$/,
        reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
        arr_url = reg_url.exec(url),
        ret = {};
      if (arr_url && arr_url[1]) {
        var str_para = arr_url[1], result;
        while ((result = reg_para.exec(str_para)) != null) {
          ret[result[1]] = result[2];
        }
      }
      return ret;
    }
  },
})
